package com.base.terminal.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.terminal.service.TerminalService;
import com.cjy.common.bean.OpAttributes;

@Action("terminalAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class TerminalAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;
	
	@Resource
	private TerminalService terminalService;
	
	private String phone;
	private int bid;
	private String code;
	
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String updateCode() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", terminalService.updateCode(code ,phone, bid)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
	
	public String getCaptcha() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", new OpAttributes().add("captcha", terminalService.getCaptcha(phone, bid))));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
}
